package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.OwnerInspectorApealDao;
import com.example.highway.dao.TaskAssignDao;
import com.example.highway.entity.Owner;
import com.example.highway.entity.OwnerInspectorApeal;
import com.example.highway.entity.TaskAssign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("owner-inspector-apeal")
public class OwnerInspectorApealController {
    @Autowired
    OwnerInspectorApealDao ownerInspectorApealDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

        OwnerInspectorApeal ownerInspectorApeal0 = jsonObject.toJavaObject(OwnerInspectorApeal.class);
        ownerInspectorApeal0.setIID("");
        ownerInspectorApeal0.setStatus(0);

        OwnerInspectorApeal ownerInspectorApeal = ownerInspectorApealDao.save(ownerInspectorApeal0);
        JSONObject rtn = new JSONObject();
        rtn.put("id", ownerInspectorApeal.getId());
        return new MyJson("0", rtn);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        if(jsonObject.containsKey("id")){
//            Optional<OwnerInspectorApeal> o = ownerInspectorApealDao.findById((String)jsonObject.get("id"));
//            if(!o.isPresent()){
//                return new MyJson("0", null);
//            }else {
//                OwnerInspectorApeal ownerInspectorApeal = o.get();
//                return new MyJson("0", ownerInspectorApeal);
//            }
//        }
//        OwnerInspectorApeal ownerInspectorApeal = jsonObject.toJavaObject(OwnerInspectorApeal.class);
//
//        List<OwnerInspectorApeal> ownerCarList = ownerInspectorApealDao.findAll(Example.of(ownerInspectorApeal));

        OwnerInspectorApeal ownerInspectorApeal = jsonObject.toJavaObject(OwnerInspectorApeal.class);
        List<OwnerInspectorApeal> ownerInspectorApeals = ownerInspectorApealDao.findAll(Example.of(ownerInspectorApeal));

        return new MyJson("0", JSONArray.toJSON(ownerInspectorApeals));

    }
}
